package webflux.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

@RestController
public class HelloController {

    @GetMapping("/hello2")
    public Mono<String> hello() {

        return Mono.just("Welcome to reactive world ~");
    }

    @GetMapping("/hello1")
    public Mono<List<String>> hello1() {
        return Mono.just(Arrays.asList("Welcome to reactive world ~", "111"));
    }
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream() {
        // 返回一个Flux对象，用于向前端推送数据
        return Flux.interval(Duration.ofSeconds(1)).map(i -> "data " + i);
    }
}
